package com.theme.passport.controller.app;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.base.DateHandler;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.controller.sys.AbstractController;
import com.theme.passport.entity.constant.*;
import com.theme.passport.entity.theme.AlbumProductEntity;
import com.theme.passport.entity.theme.RecommendProductEntity;
import com.theme.passport.service.theme.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *@name:OtherThemeController<tb>
 *@title:主题其他接口<tb>
 *@author:cuixinfu@51play.com<tb>
 *@date:2021/10/22 8:59:41<tb>
 */
@RestController
@RequestMapping("app/other-wallpaper")
@Api("其他壁纸相关接口[精品壁纸(Banner),最新壁纸,今日推荐壁纸,热门壁纸]")
public class OtherWallpaperController extends AbstractController {
    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private CarouselService carouselService;
    @Autowired
    private DownloadHistoryService downloadHistoryService;
    @Autowired
    private AlbumProductService albumProductService;
    @Autowired
    private RecommendProductService recommendProductService;

    @GetMapping("banner")
    @ApiOperation("获取轮播图壁纸(Banner)")
    public R banner(@RequestParam(value = "page", defaultValue = "1") String page,
                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", limit);
        params.put("type", String.valueOf(CarouselTypeEnum.WALLPAPER.getValue()));
        PageUtils pages = carouselService.queryPage(params);
        return R.ok().put("page", pages);
    }

    @GetMapping("newest")
    @ApiOperation("获取最新壁纸[时间排序]")
    public R newest(@RequestParam(value = "page", defaultValue = "1") String page,
                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperService.queryPage(params);
        return R.ok().put("page", pages);
    }

    @GetMapping("newProduct")
    @ApiOperation("获取新品主题壁纸[时间排序]")
    public R newProduct(@RequestParam(value = "page", defaultValue = "1") String page,
                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(status));
        params.put("nts", String.valueOf(ThemeNtsEnum.YES.getValue()));
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperService.queryPage(params);
        return R.ok().put("page", pages);
    }


    @GetMapping("todayRecommend")
    @ApiOperation("获取今日推荐壁纸")
    public R todayRecommend(@RequestParam(value = "page", defaultValue = "1") String page,
                       @RequestParam(value = "limit", defaultValue = "10") String limit) {
        List<RecommendProductEntity> recommendList = null;
        Wrapper<RecommendProductEntity> queryWrapper = new QueryWrapper<RecommendProductEntity>()
                .eq("type", AubumTypeEnum.WALLPAPER.getValue())
                .eq("recommend_id", 3);
        recommendList = recommendProductService.list(queryWrapper);
        if (recommendList == null){
            queryWrapper = new QueryWrapper<RecommendProductEntity>()
                    .eq("type", AubumTypeEnum.WALLPAPER.getValue());
            recommendList = recommendProductService.list(queryWrapper);
        }
        List<Long> wallpaperIds = new ArrayList<>(16);
        recommendList.forEach(themeRecommend ->{
            wallpaperIds.add(themeRecommend.getObjectId());
        });

        Map<String, Object> params = new HashMap<>(6);
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status", String.valueOf(status));
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperService.queryWallpaperByIds(params,wallpaperIds);

        return R.ok().put("page", pages);
    }

    @GetMapping("recommend")
    @ApiOperation("获取推荐壁纸")
    public R recommend(@RequestParam(value = "recommendId") Long recommendId,
                       @RequestParam(value = "page", defaultValue = "1") String page,
                       @RequestParam(value = "limit", defaultValue = "10") String limit) {
        List<RecommendProductEntity> recommendList = null;
        Wrapper<RecommendProductEntity> queryWrapper = new QueryWrapper<RecommendProductEntity>()
                .eq("recommend_id", recommendId);
        recommendList = recommendProductService.list(queryWrapper);
        if (recommendList == null){
            queryWrapper = new QueryWrapper<RecommendProductEntity>()
                    .eq("type", AubumTypeEnum.WALLPAPER.getValue());
            recommendList = recommendProductService.list(queryWrapper);
        }

        List<Long> wallpaperIds = new ArrayList<>(16);
        recommendList.forEach(themeRecommend ->{
            wallpaperIds.add(themeRecommend.getObjectId());
        });

        Map<String, Object> params = new HashMap<>(6);
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status", String.valueOf(status));
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperService.queryWallpaperByIds(params,wallpaperIds);

        return R.ok().put("page", pages);
    }

    @GetMapping("album")
    @ApiOperation("获取壁纸专题列表")
    public R albumList(@RequestParam(value = "albumId") Long albumId,
                       @RequestParam(value = "page", defaultValue = "1") String page,
                       @RequestParam(value = "limit", defaultValue = "10") String limit) {
        Wrapper<AlbumProductEntity> queryWrapper = new QueryWrapper<AlbumProductEntity>()
                .eq("album_id", albumId);
        List<AlbumProductEntity> albumProductList = albumProductService.list(queryWrapper);
        List<Long> wallpaperIds = new ArrayList<>(16);
        albumProductList.forEach(albumProduct ->{
            wallpaperIds.add(albumProduct.getObjectId());
        });

        Map<String, Object> params = new HashMap<>(6);
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status", String.valueOf(status));
        params.put("page", page);
        params.put("limit", limit);
        PageUtils pages = wallpaperService.queryWallpaperByIds(params,wallpaperIds);

        return R.ok().put("page", pages);
    }

    @GetMapping("hot")
    @ApiOperation("获取热门壁纸[前七天下载量最高的]")
    public R hot(@RequestParam(value = "page", defaultValue = "1") String page,
                 @RequestParam(value = "limit", defaultValue = "10") String limit) {
        //1、根据时间查询昨天24小时（一天）
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = DateHandler.addDateDays(new Date(),-7);
        Date endDate = DateHandler.addDateDays(new Date(),-1);
        Date startTime = DateHandler.getStartTime(startDate);
        Date endTime = DateHandler.getEndTime(endDate);
        Map<String, Object> hotParams = new HashMap<>();
        hotParams.put("type", String.valueOf(DownloadTypeEnum.WALLPAPER.getValue()));
        hotParams.put("startTime", formatter.format(startTime));
        hotParams.put("endTime", formatter.format(endTime));
        //2、得的主题id
        List<Long> wallPaperIds = downloadHistoryService.queryHotDownloadIds(hotParams);
        //3、根据主题id查询
        Map<String, Object> params = new HashMap<>(6);
        Integer status = ThemeStatusEnum.NORMAL.getValue();
        params.put("status", String.valueOf(status));
        params.put("page", page);
        params.put("limit", limit);
        //没有最新下的话数据正常显示几个
        if (wallPaperIds == null){
            PageUtils pages =wallpaperService.queryPage(params);
            return R.ok().put("page", pages);
        }
        PageUtils pages = wallpaperService.queryWallpaperByIds(params,wallPaperIds);
        return R.ok().put("page", pages);
    }

}
